import * as types from '../../mutation-types/power-setting/index';

const state = {
    powerSettingTeam : [],
    powerSettingList :[],
    currentSettingPage:1,
    totalSettingCount:1,
    powerModuleList : [],
    currentModulePage :1,
    totalModuleCount : 1
};

const mutations = {
	[types.POWER_SETTING_LIST] (state , {powerSettingList}){
		state.powerSettingList = powerSettingList;
	},
	[types.POWER_SETTING_LIST_CURRENTPAGE] (state , {currentSettingPage}){
		state.currentSettingPage = currentSettingPage;
	},
	[types.POWER_SETTING_LIST_TOTALCOUNT] (state , {totalSettingCount}){
		state.totalSettingCount = totalSettingCount;
	},

	[types.POWER_ITEM_LIST_ADD] (state , {list}){
		//state.powerModuleList = itemList;
		let arr = state.powerModuleList;
		for(let i = 0;i<arr.length; i++){
			if(arr[i].id == list.appModuleId){
				if(arr[i].modulResList){
					arr[i].modulResList.push(list)
				}else{
					arr[i].modulResList=list
				}
			}
		}
	},

	[types.POWER_ITEM_LIST_UPDATE] (state , {list}){
		//state.powerModuleList = itemList;
		let arr = state.powerModuleList;
		for(let i = 0;i<arr.length; i++){
			if(arr[i].id == list.appModuleId){
				for(let n = 0;n<arr[i].modulResList.length;n++){
					if(arr[i].modulResList[n].id ==list.id){
						arr[i].modulResList[n] = list;
					}
				}
			}
		}
	},

	[types.POWER_ITEM_LIST_DEL] (state , {list}){
		//state.powerModuleList = itemList;
		let arr = state.powerModuleList;
		for(let i = 0;i<arr.length; i++){
			if(arr[i].id == list.moduleId){
				for(let n = 0;n<arr[i].modulResList.length;n++){
					if(arr[i].modulResList[n].id ==list.powerId){
						arr[i].modulResList.splice(n,1)
					}
				}
			}
		}
	},

	[types.POWER_MOUDLE_LIST] (state , {powerModuleList}){
		state.powerModuleList = powerModuleList;
	},
	//添加MOUDLE
	[types.POWER_MOUDLE_ADD] (state , {list}){
		state.powerModuleList.push(list)
		state.totalModuleCount++;
	},
	//修改MOUDLE
	[types.POWER_MOUDLE_UPDATE] (state , {list}){
		state.powerModuleList.forEach((n,i)=>{
			if(n.id == list.id){
				n.name = list.name;
				n.description = list.description;
			}
		});
	},
	//删除
	[types.POWER_MOUDLE_LIST_DEL] (state , {id}){
		let arr = state.powerModuleList;
		for(let i = 0;i<arr.length; i++){
			if(arr[i].id == id){
				state.powerModuleList.splice(i,1);
			}
		}
	},
	[types.POWER_MOUDLE_LIST_CURRENTPAGE] (state , {currentModulePage}){
		state.currentModulePage = currentModulePage;
	},
	[types.POWER_MOUDLE_LIST_TOTALCOUNT] (state , {totalModuleCount}){
		state.totalModuleCount = totalModuleCount;
	},
};

export default {
	state ,
	mutations
};
